package com.bw.module_home.view

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.entity.MenuEntity
import com.bw.lib_common.view.BaseMvvmFragment
import com.bw.module_home.R
import com.bw.module_home.adapter.HomeMenuAdapter
import com.bw.module_home.adapter.HomeNoticeAdapter
import com.bw.module_home.databinding.FragmentHomeBinding
import com.bw.module_home.viewmodel.HomeViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

@Route(path = RouterPath.MODULE_HOME_FRAGMENT)
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {

    val homeNoticeAdapter = HomeNoticeAdapter()
    private val list = mutableListOf<MenuEntity>().apply {
        // 第一行
        add(MenuEntity(R.drawable.home_menu_image1, "车位管理"))      // 粉色停车标识
        add(MenuEntity(R.drawable.home_menu_image2, "访客登记"))     // 蓝底白字登记牌
        add(MenuEntity(R.drawable.home_menu_image3, "物业报修"))     // 橙色工具图标
        add(MenuEntity(R.drawable.home_menu_image4, "考勤打卡")) // 时钟图标

        // 第二行
        add(MenuEntity(R.drawable.home_menu_image5, "房源信息"))       // 带绿树的房屋
        add(MenuEntity(R.drawable.home_menu_image6, "商家推荐"))      // 店铺图案
        add(MenuEntity(R.drawable.home_menu_image7, "月卡会员"))        // VIP字样卡片
        add(MenuEntity(R.drawable.home_menu_image8, "设备管理"))    // 带百分比的齿轮
    }
    private val homeMenuAdapter = HomeMenuAdapter()
    private val vm by lazy { ViewModelProvider(this)[HomeViewModel::class.java] }
    private val list1 = mutableListOf<Int>().apply {
        add(R.drawable.banner1)
        add(R.drawable.banner2)
    }
    private var page : Int = 0
    @RequiresApi(Build.VERSION_CODES.M)
    override fun initViews() {
        super.initViews()

        //一级分类列表
        binding.homeMenuRv.adapter = homeMenuAdapter
        binding.homeMenuRv.layoutManager = GridLayoutManager(activity,4)

        //公告列表
        binding.homeRv.adapter = homeNoticeAdapter
        binding.homeRv.layoutManager = LinearLayoutManager(context)
        vm.getHomeList(0,10)
        binding.textBanner.setDatas(listOf(
            "灌灌灌灌",
            "强强强强",
            "哈哈哈哈",
            "怕怕怕怕"
        ))
        binding.textBanner.startViewAnimator()
        binding.homeBanner.setAdapter(
            object : BannerImageAdapter<Int>(list1) {
                override fun onBindView(
                    holder: BannerImageHolder,
                    data: Int,
                    position: Int,
                    size: Int
                ) {
                    Glide.with(requireActivity()).load(data).into(holder.imageView)
                }

            })
        binding.homeBanner.indicator = CircleIndicator(requireContext())
        binding.homeBanner.addBannerLifecycleObserver(this@HomeFragment)
        binding.nsv.run {
            setOnScrollChangeListener { v, _, scrollY, _, _ ->
                if (scrollY == (binding.llt.measuredHeight - v.measuredHeight)) {
                    loadMore()
                }
            }
        }
        homeMenuAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                when (position) {
                    0 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_PARKING_ACTIVITY).navigation()
                    }
                    1 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_VISIT_ACTIVITY).navigation()
                    }
                    2 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_REPAIR_ACTIVITY).navigation()
                    }
                    3 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_SIGN_ACTIVITY).navigation()
                    }
                    4 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_PROPERTY_ACTIVITY).navigation()
                    }
                    5 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_BUSINESS_PROFILE_ACTIVITY)
                            .navigation()
                    }
                    6 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_MEMBER_ACTIVITY).navigation()
                    }
                    7 -> {
                        ARouter.getInstance().build(RouterPath.MODULE_DEVICE_ACTIVITY).navigation()
                    }
                }
            }
        })
    }
    private fun loadMore(){
        page+=1
        vm.getHomeList(page,5)
    }
    override fun loadData(){
        homeMenuAdapter.data.addAll(list)
        homeMenuAdapter.notifyDataSetChanged()

        vm.mSuccessLiveData.observe(this){
            homeNoticeAdapter.data.addAll(it.reversed())
            homeNoticeAdapter.notifyDataSetChanged()
        }
    }

    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.fragment_home


}